/* ==============================================================================
*
*   Copyright (c) 2010, Pierre Favier
*   All rights reserved.
*
*   NTasking is free software: you can redistribute it and/or modify it
*   under the terms of the GNU General Public License as published by the
*   Free Software Foundation.
*
*   Redistribution and use in source and binary forms, with or without
*   modification, are permitted provided that the following conditions
*   are met:
*
*   -   Redistributions of source code must retain the above copyright notice,
*       this list of conditions and the following disclaimer.
*
*   -   Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials
*       provided with the distribution.
*
*   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
*   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
*   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
*   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
*   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
*   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
*   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
*   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*   SUCH DAMAGE.
*
* ============================================================================== */

using System;
using NUnit.Framework;
using Rhino.Mocks;
using Rhino.Mocks.Constraints;

namespace NTasking.ValidationTests.Other.SyntaxErrors
{

    [TestFixture]
    public class Protecteds : ValidationTest
    {

        internal class InvalidReadWriteCallProtected : Protected
        {
            [Entry]
            private Action AnEntry { get; set; }


            [ReadWriteOperation]
            private Action AnRWOperation { get; set; }


            [ReadOnlyOperation]
            internal Func<bool> ROOperation { get; private set; }


            internal InvalidReadWriteCallProtected()
            {
                Entry(AnEntry, When: () => true, Is: () => { });

                ReadWriteOperation(AnRWOperation, Is: () => { });

                ReadOnlyOperation(ROOperation, Is: () =>
                {
                    AnRWOperation();
                    return true;
                });
            }

        }


        [Test]
        public void InvalidReadWriteCall()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            InvalidReadWriteCallProtected prot = null;

            Ada.Declare(() =>
            {
                prot = new InvalidReadWriteCallProtected();

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();
            },
            Begin: () =>
            {
                try
                {
                    prot.ROOperation();
                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.InvalidReadWriteCallError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            },
            Exception: (ex) =>
            {
                // Unexpected exception thrown
                result.TestFailed();

                return false;
            });
        }


        internal class UninitializedOperationProtected : Protected
        {
            [Entry]
            internal Action AnEntry { get; private set; }


            [ReadWriteOperation]
            internal Action AnRWOperation { get; private set; }


            [ReadOnlyOperation]
            internal Func<bool> AnROOperation { get; private set; }


            internal UninitializedOperationProtected()
            {
            }

        }


        [Test]
        public void UninitializedOperation()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            UninitializedOperationProtected prot = null;

            Ada.Declare(() =>
            {
                prot = new UninitializedOperationProtected();

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();
            },
            Begin: () =>
            {
                try
                {
                    prot.AnROOperation();
                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.UninitializedProtectedOperationError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    prot.AnRWOperation();
                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.UninitializedProtectedOperationError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    prot.AnEntry();
                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.UninitializedProtectedOperationError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            },
            Exception: (ex) =>
            {
                // Unexpected exception thrown
                result.TestFailed();

                return false;
            });
        }


        internal class InitializationNotInConstructorProtected : Protected
        {
            [Entry]
            internal Action AnEntry { get; private set; }


            [ReadWriteOperation]
            internal Action AnRWOperation { get; private set; }


            [ReadOnlyOperation]
            internal Func<bool> AnROOperation { get; private set; }


            internal InitializationNotInConstructorProtected()
            {
            }

            internal void Initialize1()
            {
                ReadOnlyOperation(AnROOperation, Is: () => true);
            }

            internal void Initialize2()
            {
                ReadWriteOperation(AnRWOperation, Is: () => { });
            }

            internal void Initialize3()
            {
                Entry(AnEntry, When: () => true, Is: () => { });
            }
        }


        [Test]
        public void InitializationNotInConstructor()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            InitializationNotInConstructorProtected prot = null;

            Ada.Declare(() =>
            {
                prot = new InitializationNotInConstructorProtected();

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();
            },
            Begin: () =>
            {
                try
                {
                    prot.Initialize1();
                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.InitializationNotInConstructorError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    prot.Initialize2();
                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.InitializationNotInConstructorError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    prot.Initialize3();
                    // Exception not thrown
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.InitializationNotInConstructorError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            },
            Exception: (ex) =>
            {
                // Unexpected exception thrown
                result.TestFailed();

                return false;
            });
        }


        internal class EntryOrProtectedOperationIsNullProtected : ValidationTestProtected
        {

            internal EntryOrProtectedOperationIsNullProtected(IValidationTestResult result)
                : base(result)
            {
                Action anEntry = null;
                Func<bool> anOperation = null;
                ActionFamily<int> aFamily = null;

                try
                {
                    Entry(anEntry, When: () => true, Is: () => { });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.EntryIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    Entry(aFamily, When: () => true, Is: (i) => { });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.EntryIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    Entry(aFamily, When: (i) => true, Is: (i) => { });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.EntryIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadWriteOperation(anOperation, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.EntryIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadOnlyOperation(anOperation, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.EntryIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            }

        }


        [Test]
        public void EntryOrProtectedOperationIsNull()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            EntryOrProtectedOperationIsNullProtected prot = null;

            Ada.Declare(() =>
            {
                prot = new EntryOrProtectedOperationIsNullProtected(result);

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();
            },
            Begin: () =>
            {
            },
            Exception: (ex) =>
            {
                // Unexpected exception thrown
                result.TestFailed();

                return false;
            });
        }


        internal class EntryOrProtectedOperationBodyIsNullProtected : ValidationTestProtected
        {

            [Entry]
            private Action anEntry { get; set; }


            [Entry]
            private ActionFamily<int> aFamily { get; set; }


            [ReadOnlyOperation]
            private Func<bool> aRoOperation { get; set; }


            [ReadWriteOperation]
            private Func<bool> aRwOperation { get; set; }


            internal EntryOrProtectedOperationBodyIsNullProtected(IValidationTestResult result)
                : base(result)
            {

                try
                {
                    Entry(anEntry, When: () => true, Is: null);
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ProtectedBodyIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    Entry(aFamily, When: () => true, Is: null);
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ProtectedBodyIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    Entry(aFamily, When: (i) => true, Is: null);
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ProtectedBodyIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadWriteOperation(aRwOperation, Is: null);
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ProtectedBodyIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadOnlyOperation(aRoOperation, Is: null);
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.ProtectedBodyIsNullError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            }

        }


        [Test]
        public void EntryOrProtectedOperationBodyIsNull()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            EntryOrProtectedOperationBodyIsNullProtected prot = null;

            Ada.Declare(() =>
            {
                prot = new EntryOrProtectedOperationBodyIsNullProtected(result);

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();
            },
            Begin: () =>
            {
            },
            Exception: (ex) =>
            {
                // Unexpected exception thrown
                result.TestFailed();

                return false;
            });
        }


        internal class OperationNotSynchronizedProtected : ValidationTestProtected
        {

            private bool AMethod()
            {
                return true;
            }


            internal OperationNotSynchronizedProtected(IValidationTestResult result)
                : base(result)
            {

                Func<bool> aMethod = AMethod;

                try
                {
                    Entry(aMethod, When: () => true, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.NotASynchronizedMethodError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadWriteOperation(aMethod, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.NotASynchronizedMethodError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadOnlyOperation(aMethod, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.NotASynchronizedMethodError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            }

        }


        [Test]
        public void OperationNotSynchronized()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            OperationNotSynchronizedProtected prot = null;

            Ada.Declare(() =>
            {
                prot = new OperationNotSynchronizedProtected(result);

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();
            },
            Begin: () =>
            {
            },
            Exception: (ex) =>
            {
                // Unexpected exception thrown
                result.TestFailed();

                return false;
            });
        }


        internal class OwnedProtected : Protected
        {

            [Entry]
            internal Action anEntry { get; private set; }


            [Entry]
            internal ActionFamily<int> aFamily { get; private set; }


            [ReadOnlyOperation]
            internal Func<bool> aRoOperation { get; private set; }


            [ReadWriteOperation]
            internal Func<bool> aRwOperation { get; private set; }


            internal OwnedProtected()
            {
            }

        }


        internal class EntryOrProtectedOperationNotOwnedProtected : ValidationTestProtected
        {

            internal EntryOrProtectedOperationNotOwnedProtected(IValidationTestResult result, OwnedProtected _other)
                : base(result)
            {

                try
                {
                    Entry(_other.anEntry, When: () => true, Is: () => {});
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.NotAMemberError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    Entry(_other.aFamily, When: () => true, Is: (i) => {});
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.NotAMemberError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    Entry(_other.aFamily, When: (i) => true, Is: (i) => {});
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.NotAMemberError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadWriteOperation(_other.aRwOperation, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.NotAMemberError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadOnlyOperation(_other.aRoOperation, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.NotAMemberError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            }

        }


        [Test]
        public void EntryOrProtectedOperationNotOwned()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            OwnedProtected other = null;
            EntryOrProtectedOperationNotOwnedProtected prot = null;

            Ada.Declare(() =>
            {
                other = new OwnedProtected();
                prot = new EntryOrProtectedOperationNotOwnedProtected(result, other);

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();
            },
            Begin: () =>
            {
            },
            Exception: (ex) =>
            {
                // Unexpected exception thrown
                result.TestFailed();

                return false;
            });
        }


        internal class FamilyMemberInitializationProtected : ValidationTestProtected
        {

            [Entry]
            internal FuncFamily<int, bool> aFamily { get; private set; }


            internal FamilyMemberInitializationProtected(IValidationTestResult result)
                : base(result)
            {

                try
                {
                    Entry(aFamily[1], When: () => true, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.FamilyMemberNotAllowedError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadWriteOperation(aFamily[1], Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.FamilyMemberNotAllowedError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadOnlyOperation(aFamily[1], Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.FamilyMemberNotAllowedError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            }

        }


        [Test]
        public void FamilyMemberInitialization()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            FamilyMemberInitializationProtected prot = null;

            Ada.Declare(() =>
            {
                prot = new FamilyMemberInitializationProtected(result);

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();
            },
            Begin: () =>
            {
            },
            Exception: (ex) =>
            {
                // Unexpected exception thrown
                result.TestFailed();

                return false;
            });
        }


        internal class ConflictWithAttributeProtected : ValidationTestProtected
        {

            [Entry]
            internal Func<bool> anEntry { get; private set; }


            [ReadOnlyOperation]
            internal Func<bool> aRoOperation { get; private set; }


            [ReadWriteOperation]
            internal Func<bool> aRwOperation { get; private set; }


            internal ConflictWithAttributeProtected(IValidationTestResult result)
                : base(result)
            {

                try
                {
                    Entry(aRoOperation, When: () => true, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.DeclarationConflictsWithAttributeError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    Entry(aRwOperation, When: () => true, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.DeclarationConflictsWithAttributeError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadWriteOperation(anEntry, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.DeclarationConflictsWithAttributeError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadWriteOperation(aRoOperation, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.DeclarationConflictsWithAttributeError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadOnlyOperation(anEntry, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.DeclarationConflictsWithAttributeError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }

                try
                {
                    ReadOnlyOperation(aRwOperation, Is: () => { return true; });
                    result.TestFailed();
                }
                catch (SyntaxError ex)
                {
                    if (ex.ErrorCode != Errors.DeclarationConflictsWithAttributeError)
                        // Wrong error code
                        result.TestFailed();
                }
                catch (Exception)
                {
                    // Wrong exception thrown
                    result.TestFailed();
                }
            }

        }


        [Test]
        public void ConflictWithAttribute()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            ConflictWithAttributeProtected prot = null;

            Ada.Declare(() =>
            {
                prot = new ConflictWithAttributeProtected(result);

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();

                Mocks.ReplayAll();
            },
            Begin: () =>
            {
            },
            Exception: (ex) =>
            {
                // Unexpected exception thrown
                result.TestFailed();

                return false;
            });
        }

    }

}
